package com.hpch.business.controller;

import cn.hutool.core.util.StrUtil;
import com.hpch.common.core.web.BaseController;
import com.hpch.business.service.ArticleService;
import com.hpch.business.entity.Article;
import com.hpch.business.param.ArticleParam;
import com.hpch.common.core.web.ApiResult;
import com.hpch.common.core.web.PageResult;
import com.hpch.common.core.web.PageParam;
import com.hpch.common.core.web.BatchParam;
import com.hpch.common.core.annotation.OperationLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 单页内容表控制器
 *
 * @author EleAdmin
 * @since 2025-03-02 19:12:18
 */
@Api(tags = "单页内容表管理")
@RestController
@RequestMapping("/api/business/article")
public class ArticleController extends BaseController {
    @Resource
    private ArticleService articleService;

    @PreAuthorize("hasAuthority('business:article:list')")
    @OperationLog
    @ApiOperation("分页查询单页内容表")
    @GetMapping("/page")
    public ApiResult<PageResult<Article>> page(ArticleParam param) {
        PageParam<Article, ArticleParam> page = new PageParam<>(param);
        //page.setDefaultOrder("create_time desc");
//        return success(articleService.page(page, page.getWrapper()));
        // 使用关联查询
        return success(articleService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('business:article:list')")
    @OperationLog
    @ApiOperation("查询全部单页内容表")
    @GetMapping()
    public ApiResult<List<Article>> list(ArticleParam param) {
        PageParam<Article, ArticleParam> page = new PageParam<>(param);
        //page.setDefaultOrder("create_time desc");
//        return success(articleService.list(page.getOrderWrapper()));
        // 使用关联查询
        return success(articleService.listRel(param));
    }

    @PreAuthorize("hasAuthority('business:article:list')")
    @OperationLog
    @ApiOperation("根据id查询单页内容表")
    @GetMapping("/{id}")
    public ApiResult<Article> get(@PathVariable("id") Integer id) {
//        return success(articleService.getById(id));
        // 使用关联查询
        return success(articleService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('business:article:save')")
    @OperationLog
    @ApiOperation("添加单页内容表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Article article) {
        // 标题 图片 简介 内容 必填
        if (StrUtil.hasBlank(article.getTitle(), article.getThumb(), article.getContent())) {
            return fail("标题 图片 简介 内容 不能为空");
        }
        if (articleService.edit(article)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('business:article:update')")
    @OperationLog
    @ApiOperation("修改单页内容表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Article article) {
        if (article.getId() == null){
            return fail("id 不能为空");
        }
        if (StrUtil.hasBlank(article.getTitle(), article.getThumb(), article.getContent())) {
            return fail("标题 图片 简介 内容 不能为空");
        }
        if (articleService.edit(article)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('business:article:remove')")
    @OperationLog
    @ApiOperation("删除单页内容表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (articleService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('business:article:save')")
    @OperationLog
    @ApiOperation("批量添加单页内容表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Article> list) {
        if (articleService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('business:article:update')")
    @OperationLog
    @ApiOperation("批量修改单页内容表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Article> batchParam) {
        if (batchParam.update(articleService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('business:article:remove')")
    @OperationLog
    @ApiOperation("批量删除单页内容表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (articleService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }


    /**
     * status 修改状态
     */
    @PreAuthorize("hasAuthority('business:article:update')")
    @OperationLog
    @ApiOperation("修改状态")
    @PutMapping("/status")
    public ApiResult<?> updateStatus(@RequestBody Article article) {
        if (article.getId() == null || article.getStatusFlag() == null) {
            return fail("参数不能为空");
        }
        if (articleService.updateById(article)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }


    /**
     * status 修改状态
     */
    @PreAuthorize("hasAuthority('business:article:update')")
    @OperationLog
    @ApiOperation("修改状态")
    @PutMapping("/recommend")
    public ApiResult<?> recommend(@RequestBody Article article) {
        if (articleService.recommend(article)) {
            return success("操作成功");
        }
        return fail("修改失败");
    }


}
